package com.example.project22_editingmediaplayer;

import java.util.ArrayList;

import sharevideo.ShareVideo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.project22_editingmediaplayer.action.AudioUltility;
import com.example.project22_editingmediaplayer.adapter.RowVideo;
import com.example.project22_editingmediaplayer.objects.VideoObject;

public class LayoutGenres extends Fragment {
	Context myContext;
	ListView listview_video;
	ArrayList<VideoObject> listvideo=new ArrayList<VideoObject>();
	AudioUltility audi;
	private RowVideo rowAdapter;
	private Activity myActivity;
	public static Fragment newInstance(Context context) {
		LayoutGenres f = new LayoutGenres();
		return f;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_genres,
				null);
		myContext = container.getContext();
		return root;
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		myActivity=this.getActivity();
		listview_video=(ListView) myActivity.findViewById(R.id.list_video);
		audi=new AudioUltility(myContext);
		rowAdapter=new RowVideo(listvideo, myContext);
		listview_video.setAdapter(rowAdapter);
		new LoadAllVideo().execute();
		listview_video.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(myActivity,ShareVideo.class);
				VideoObject vo=(VideoObject) listview_video.getItemAtPosition(arg2);
				intent.putExtra("key",vo.get_path());
				myActivity.startActivity(intent);
			}
		});
		
	}

	public class LoadAllVideo extends
			AsyncTask<Void, Void, ArrayList<VideoObject>> {
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub

			super.onPreExecute();
		}

		@Override
		protected ArrayList<VideoObject> doInBackground(Void... params) {
			// TODO Auto-generated method stub
			return audi.getAllVideo();
		}

		@Override
		protected void onPostExecute(ArrayList<VideoObject> result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			rowAdapter.resetData(result);

		}

	}
}
